﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace ViewModel.Aggregators
{
    /// <summary>
    /// Calculates the Balance of a savings plan
    /// </summary>
    public class SavingsBalanceProvider : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime startTime = (DateTime)values[0];
                DateTime endTime = (DateTime)values[1];
                DateTime currentTime = DateTime.Today;
                decimal targetAmount = (decimal)values[2];
                decimal initialAmount = (decimal)values[3];
                decimal retrievedAmount = (decimal)values[4];
                return SavingsPlanCalculator.CalculateBalance(startTime, endTime, currentTime,
                                                              targetAmount, initialAmount, retrievedAmount).ToString("C");
            }
            catch (InvalidCastException e)
            {
                log4net.LogManager.GetLogger(GetType()).Error("Could not convert properties to a decimals", e);
                return null;
            }
            catch(DivideByZeroException d1)
            {
                log4net.LogManager.GetLogger(GetType()).Error("Attempted to divide by zero. Possibly FrequencyItem value is zero", d1);
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
